package com.ruoyi.channel.service.impl;

import java.util.List;

import com.ruoyi.channel.domain.MpChannelIp;
import com.ruoyi.channel.mapper.MpChannelIpMapper;
import com.ruoyi.channel.service.IMpChannelIpService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;

/**
 *  服务层实现
 * 
 * @author ruoyi
 * @date 2019-05-07
 */
@Service
public class MpChannelIpServiceImpl implements IMpChannelIpService
{
	/** 渠道名称是否唯一的返回结果 */
	public final static String MPCHANNEL_IP_UNIQUE = "0";
	public final static String MPCHANNEL_IP_NOT_UNIQUE = "1";

	@Autowired
	private MpChannelIpMapper mpChannelIpMapper;

	/**
     * 查询信息
     * 
     * @param id ID
     * @return 信息
     */
    @Override
	public MpChannelIp selectMpChannelIpById(Long id)
	{
	    return mpChannelIpMapper.selectMpChannelIpById(id);
	}
	
	/**
     * 查询列表
     * 
     * @param mpChannelIp 信息
     * @return 集合
     */
	@Override
	public List<MpChannelIp> selectMpChannelIpList(MpChannelIp mpChannelIp)
	{
	    return mpChannelIpMapper.selectMpChannelIpList(mpChannelIp);
	}
	
    /**
     * 新增
     * 
     * @param mpChannelIp 信息
     * @return 结果
     */
	@Override
	public int insertMpChannelIp(MpChannelIp mpChannelIp)
	{
	    return mpChannelIpMapper.insertMpChannelIp(mpChannelIp);
	}
	
	/**
     * 修改
     * 
     * @param mpChannelIp 信息
     * @return 结果
     */
	@Override
	public int updateMpChannelIp(MpChannelIp mpChannelIp)
	{
	    return mpChannelIpMapper.updateMpChannelIp(mpChannelIp);
	}

	/**
     * 删除对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteMpChannelIpByIds(String ids)
	{
		return mpChannelIpMapper.deleteMpChannelIpByIds(Convert.toStrArray(ids));
	}


	/**
	 * 校验渠道是否添加了该Ip
	 *
	 * @param mpChannelIp 渠道ip黑白名单
	 * @return 结果
	 */
	@Override
	public String checkIpUnique(MpChannelIp mpChannelIp) {
		Long mpChannelIpId = StringUtils.isNull(mpChannelIp.getId()) ? -1L : mpChannelIp.getId();
		MpChannelIp info = mpChannelIpMapper.checkIpUnique(mpChannelIp.getChannelId(), mpChannelIp.getIp());
		if (StringUtils.isNotNull(info) && info.getId().longValue() != mpChannelIpId) {
			return MPCHANNEL_IP_NOT_UNIQUE;
		}
		return MPCHANNEL_IP_UNIQUE;
	}
}
